<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/logic/factory/CommandFactory.php';
	
	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];
	
	class PresenterResource {
		/**
		 * Constructor of the presenter
		 */
		public function __construct() {}
		
		/**
		 * Method to execute to create a new resources request
		 */
		public function clickCreateResourcesRequest($resources, $requester) {
			$command = CommandFactory :: createCommandCreateResourcesRequest
				(date("Y-m-d", time()), $requester);
			$command -> execute();
			
			if ($command -> getResult() == 0)
				return 0;
			
			$resourcesExplode = explode("\n", $resources);
			for ($i = 0; $i < count($resourcesExplode); $i++) {
				$resource = $resourcesExplode[$i];
				$resourceExplode = explode(", ", $resource);						
				$category = $resourceExplode[0];
				
				$resourceDescription = explode(" (", $resourceExplode[1]);
				$identifier = $resourceDescription[0];
				$specifications = '';
				if (strcmp($resourceDescription[1], '') != 0)
					$specifications = substr($resourceDescription[1], 0, 
						strlen($resourceDescription[1]) - 1);
				
				$resourceCost = explode(' ', $resourceExplode[2]);
				$cost = $resourceCost[0];
				
				$GLOBALS['debug'] -> log($category . ' ' . $identifier . ' ' . $specifications . ' ' . $cost . ' ' . $requester, "Resource description");
				
				$command = CommandFactory :: createCommandCreateResource($category, $identifier, 
					$specifications, $cost);
				$command -> execute();
				
				$GLOBALS['debug'] -> log($i, "i");
			}
		}
		
		/**
		 * Method to execute to select Resource
		 */
		public function selectResources() {
					$GLOBALS['debug']->log('Inicio funcion select resource--presentador');
			$command = CommandFactory :: createCommandSelectResources();
			
			$command -> execute();
			$resource = $command -> getResult();
			
			$result = '<table class="formTable" cellspacing="0" cellpadding="5">';
			$result .= '<tr class="odd">';
			
		
			$result .= '<td align="center"><b>Nombre</b></td>';
			$result .= '<td align="center"><b>Apellido</b></td>';
			$result .= '<td align="center"><b>Recurso</b></td>';
			$result .= '<td align="center"><b>Categoria</b></td>';
			$result .= '<td align="center"><b>Costo</b></td>';

			$result .= '</tr>';
			$i = 0;
			while ($i < count($resource['pk_id'])) {
				if ($i % 2 == 0) $result .= '<tr class="even">';
				else $result .= '<tr class="odd">';
				
		$GLOBALS['debug']->log($resource['first_name'][$i],'primer nombre');
		$GLOBALS['debug']->log($resource['first_last_name'][$i],'primer apellido');
		$GLOBALS['debug']->log( $resource['name'][$i] ,'recurso');
		$GLOBALS['debug']->log($resource['category'][$i] ,'nombre de la categoria');
		$GLOBALS['debug']->log($resource['cost'][$i],'costo');
		
				
				$result .= '<td align="center">' . $resource['first_name'][$i] . '</td>';
				$result .= '<td align="center">' . $resource['first_last_name'][$i] . '</td>';
				$result .= '<td align="center">' . $resource['name'][$i] . '</td>';
				$result .= '<td align="center">' . $resource['category'][$i] . '</td>';
				$result .= '<td align="center">' . $resource['cost'][$i++] . '</td>';
	
			

				$result .= '</tr>';
			}
			

			$result .= '</table>';
		$GLOBALS['debug']->log('fin funcion select resource--presentador');

			return $result;
		}
	}
?>